package com.jackstraw.jack_base.datasource.mybatisplus.conf;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.jackstraw.jack_base.util.SpringUtil;
import com.jackstraw.jack_base.util.condition.ConditionalOnHavingProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import javax.sql.DataSource;

@Slf4j
@Order(Ordered.HIGHEST_PRECEDENCE)
@Configuration
@MapperScan(basePackages = {"com.jackstraw.**.dao.database"})
public class MyBatisPlusConfig {

    public MyBatisPlusConfig(ApplicationContext applicationContext) {
        SpringUtil.setApplicationContextJack(applicationContext);
    }


    @Bean
    @Primary
    @ConfigurationProperties(prefix = "jack.datasource.mybatis-plus")
    @ConditionalOnHavingProperties(prefix = "jack.datasource", value = "mybatis-plus")
    public MybatisPlusProperties mybatisPlusProperties() {
        return new MybatisPlusProperties();
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource, MybatisPlusProperties mybatisPlusProperties) {
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(dataSource);
        mybatisSqlSessionFactoryBean.setTypeAliasesPackage(mybatisPlusProperties.getTypeAliasesPackage());
        // 设置mybatis的xml所在位置
        mybatisSqlSessionFactoryBean.setMapperLocations(mybatisPlusProperties.resolveMapperLocations());
        mybatisSqlSessionFactoryBean.setConfiguration(mybatisPlusProperties.getConfiguration());
        try {
            return mybatisSqlSessionFactoryBean.getObject();
        } catch (Exception e) {
            log.error("创建SqlSessionFactory异常，异常原因为：{}", e);
            return new DefaultSqlSessionFactory(mybatisPlusProperties.getConfiguration());
        }
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory,
                                                 MybatisPlusProperties mybatisPlusProperties) {

        ExecutorType executorType = mybatisPlusProperties.getExecutorType();
        if (executorType != null) {
            return new SqlSessionTemplate(sqlSessionFactory, executorType);
        } else {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }

}